package com.yunpeng.aidevice.common.constant;

import com.yunpeng.aidevice.common.dto.AopRequestDTO;
import com.yunpeng.aidevice.common.dto.cache.LoginUserCacheDTO;
import com.yunpeng.aidevice.common.enums.ResultCodeEnum;
import com.yunpeng.common.exception.UnLoginException;
import com.yunpeng.common.util.AppConfigUtil;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;


/**
 * ConfigConstantValues 配置类常量数据
 *
 * @author : Moore(hemw)
 * @date : 2020-03-14
 */
public class ConfigConstantValues {

    /**
     * 请求的对象封装
     */
    public final static ThreadLocal<AopRequestDTO> THREAD_AOP_REQUEST_OBJECT = new ThreadLocal<>();

    /**
     * 默认清理
     */
    static {
        THREAD_AOP_REQUEST_OBJECT.remove();
    }

    /**
     * 取ip用
     */
    public final static String UNKNOWN = "unknown";
    /**
     * 取ip用
     */
    public final static String X_FORWARDED_FOR = "X-Forwarded-For";
    /**
     * 取ip用
     */
    public final static String PROXY_CLIENT_IP = "Proxy-Client-IP";
    /**
     * 取ip用
     */
    public final static String WL_PROXY_CLIENT_IP = "WL-Proxy-Client-IP";
    /**
     * 取ip用
     */
    public final static String HTTP_CLIENT_IP = "HTTP_CLIENT_IP";
    /**
     * 取ip用
     */
    public final static String HTTP_X_FORWARDED_FOR = "HTTP_X_FORWARDED_FOR";
    /**
     * 取ip用
     */
    public final static int IP_LENGTH = 15;
    /**
     * 应用抛出html类异常的自定义视图名称（404错误等）
     */
    public final static String ERROR_PAGE_VIEW = "error";

    /**
     * 本地ip
     */
    public final static String LOCALHOST = "127.0.0.1";

    /**
     * 本地ip
     */
    public final static String LOCAL_ADDRESS = "0:0:0:0:0:0:0:1";

    /**
     * 默认charset 为utf-8
     */
    public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;


    /**
     * accessToken 参数名
     */
    public static final String ACCESS_TOKEN = "accessToken";

    /**
     * 获取当前的缓存登录用户信息
     *
     * @return
     */
    public static LoginUserCacheDTO getLoginUserCacheDTO() {
        AopRequestDTO aopRequestDTO = ConfigConstantValues.THREAD_AOP_REQUEST_OBJECT.get();
        if (aopRequestDTO.getLoginUserCacheDTO() == null) {
            throw new UnLoginException(ResultCodeEnum.USER_NO_LOGIN);
        }
        return aopRequestDTO.getLoginUserCacheDTO();
    }

    /**
     * 当前服务器序号
     */
    public static final String CURRENT_SERVER_MACHINE_NUMBER = AppConfigUtil.getProperties("machine.number");

    /**
     * 规则参数字典域
     */
    public static final String RULE_PARAMETER_DOMAIN = "ruleParameter";

    /**
     * 系统配置域
     */
    public static final String SYSTEM_DOMAIN = "system";

    /**
     * app更新组
     */
    public static final String APP_UPDATE_GROUP = "appUpdate";

    /**
     * 默认邮件传输协议
     */
    public static final String DEFAULT_MAIL_PROTOCOL = "smtp";
    /**
     * 应用名称
     */
    public static final String APPLICATION_NAME = "japan_pad";
}
